﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using Reader.Libs.BOOKOBJ;
using Reader.Libs.CONVERTER;

namespace Reader.UC.Dialogs
{
    /// <summary>
    /// Interaction logic for PdfPasswordDialog.xaml
    /// </summary>
    public partial class ConvertDialog : MetroWindow
    {
        public ConvertDialog(Book b)
        {
            InitializeComponent();
            if (b != null)
                if (System.IO.File.Exists(b.FilePath))
                {
                    this.Title.Text = "Convert Book: " + b.FullName;
                    this.SourceTb.Text = b.FilePath;
                    this.SaveToTb.Text = System.IO.Path.GetDirectoryName(b.FilePath);
                    string fileExt = System.IO.Path.GetExtension(b.FilePath);

                    FixTargetType(fileExt);
                }
        }

        private void FixTargetType(string fileExt)
        {
            string s = "";
            switch (fileExt.ToLower())
            {
                case ".txt":
                    s = "cbiTXT";
                    break;
                case ".doc":
                    s = "cbiDOC";
                    break;
                case ".docx":
                    s = "cbiDOCX";
                    break;
                case ".pdf":
                    s = "cbiPDF";
                    break;
                case ".html":
                    s = "cbiHTML";
                    break;
                case ".mobi":
                    s = "cbiMOBI";
                    break;
                case ".prc":
                    s = "cbiprc";
                    break;
            }

            foreach (ComboBoxItem i in this.ConvertToCb.Items)
            {
                if(!i.Name.Equals(s))
                {
                    if (i.Visibility == Visibility.Collapsed)
                    i.Visibility = Visibility.Visible;
                }
                else
                {
                    i.Visibility = Visibility.Collapsed;
                }
                
            }
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void BrowSrcBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog OfDlg = new Microsoft.Win32.OpenFileDialog();
                OfDlg.Filter = "Ebook files |*.txt; *.pdf; *.html; *.htm; *.doc; *.docx; *.mobi; *.prc" +
                               "|PDF File |*.pdf" + "|Web Page |*.html; *.htm" +
                               "|Word File |*.doc; *.docx" + "|MobiPocKet |*.prc; *.mobi";
                OfDlg.Multiselect = false;
                OfDlg.ShowDialog();

                this.SourceTb.Text = OfDlg.FileName;
                this.Title.Text = "Convert Book: " + System.IO.Path.GetFileNameWithoutExtension(OfDlg.FileName.ToLower());
                this.SaveToTb.Text = System.IO.Path.GetDirectoryName(OfDlg.FileName.ToLower());
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! " + ex.Message);
            }
        }

        private void BrowSvtBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dialog = new System.Windows.Forms.FolderBrowserDialog();
                dialog.ShowNewFolderButton = true;
                System.Windows.Forms.DialogResult result = dialog.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                    this.SaveToTb.Text = dialog.SelectedPath;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! " + ex.Message);
            }
        }

        private void SourceTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (System.IO.File.Exists(this.SourceTb.Text.ToLower()))
            {
                string fileExt = System.IO.Path.GetExtension(this.SourceTb.Text.ToLower());
                FixTargetType(fileExt);
            }
            
        }

        private void ConvertBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (System.IO.File.Exists(SourceTb.Text))
                {
                    string fileExt = System.IO.Path.GetExtension(SourceTb.Text);
                    switch (fileExt)
                    {
                        case ".txt":
                            TxtConvert();
                            break;
                        case ".doc":
                            DocConvert();
                            break;
                        case ".docx":
                            DocXConvert();
                            break;
                        case ".pdf":
                            PdfConvert();
                            break;
                        case ".htm":
                        case ".html":
                            HtmlConvert();
                            break;
                        case ".prc":
                        case ".mobi":
                            MobiConvert();
                            break;
                    }
                }

                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! " + ex.Message);
            }
        }

        private void TxtConvert()
        {
            try
            {
                string fileExt = System.IO.Path.GetExtension(SourceTb.Text);
                string fileToSave = this.SaveToTb.Text + "\\" + System.IO.Path.GetFileNameWithoutExtension(SourceTb.Text);
                if (fileExt == ".txt")
                {
                    TxtConverter c = new TxtConverter();
                    string ct = (this.ConvertToCb.SelectedItem as ComboBoxItem).Name;
                    switch (ct)
                    {
                        case "cbiDOC":
                            fileToSave += ".doc";
                            c.ConvertToDoc(SourceTb.Text, fileToSave);
                            break;
                        case "cbiDOCX":
                            fileToSave += ".docx";
                            c.ConvertToDocX(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPDF":
                            fileToSave += ".pdf";
                            c.ConvertToPdf(SourceTb.Text, fileToSave);
                            break;
                        case "cbiHTML":
                            fileToSave += ".html";
                            c.ConvertToHtml(SourceTb.Text, fileToSave);
                            break;
                        case "cbiMOBI":
                            fileToSave += ".mobi";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPRC":
                            fileToSave += ".prc";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                    }
                }
            }
            catch { throw; }
        }

        private void DocConvert()
        {
            try
            {
                string fileExt = System.IO.Path.GetExtension(SourceTb.Text);
                string fileToSave = this.SaveToTb.Text + "\\" + System.IO.Path.GetFileNameWithoutExtension(SourceTb.Text);
                if (fileExt == ".doc")
                {
                    DocConverter c = new DocConverter();
                    string ct = (this.ConvertToCb.SelectedItem as ComboBoxItem).Name;
                    switch (ct)
                    {
                        case "cbiTXT":
                            fileToSave += ".txt";
                            c.ConvertToTxt(SourceTb.Text, fileToSave);
                            break;
                        case "cbiDOCX":
                            fileToSave += ".docx";
                            c.ConvertToDocX(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPDF":
                            fileToSave += ".pdf";
                            c.ConvertToPdf(SourceTb.Text, fileToSave);
                            break;
                        case "cbiHTML":
                            fileToSave += ".html";
                            c.ConvertToHtml(SourceTb.Text, fileToSave);
                            break;
                        case "cbiMOBI":
                            fileToSave += ".mobi";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPRC":
                            fileToSave += ".prc";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                    }
                }
            }
            catch { throw; }
        }

        private void DocXConvert()
        {
            try
            {
                string fileExt = System.IO.Path.GetExtension(SourceTb.Text);
                string fileToSave = this.SaveToTb.Text + "\\" + System.IO.Path.GetFileNameWithoutExtension(SourceTb.Text);
                if (fileExt == ".docx")
                {
                    DocxConverter c = new DocxConverter();
                    string ct = (this.ConvertToCb.SelectedItem as ComboBoxItem).Name;
                    switch (ct)
                    {
                        case "cbiTXT":
                            fileToSave += ".txt";
                            c.ConvertToTxt(SourceTb.Text, fileToSave);
                            break;
                        case "cbiDOC":
                            fileToSave += ".doc";
                            c.ConvertToDoc(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPDF":
                            fileToSave += ".pdf";
                            c.ConvertToPdf(SourceTb.Text, fileToSave);
                            break;
                        case "cbiHTML":
                            fileToSave += ".html";
                            c.ConvertToHtml(SourceTb.Text, fileToSave);
                            break;
                        case "cbiMOBI":
                            fileToSave += ".mobi";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPRC":
                            fileToSave += ".prc";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                    }
                }
            }
            catch { throw; }
        }

        private void PdfConvert()
        {
            try
            {
                string fileExt = System.IO.Path.GetExtension(SourceTb.Text);
                string fileToSave = this.SaveToTb.Text + "\\" + System.IO.Path.GetFileNameWithoutExtension(SourceTb.Text);
                if (fileExt == ".pdf")
                {
                    PdfConverter c = new PdfConverter();
                    string ct = (this.ConvertToCb.SelectedItem as ComboBoxItem).Name;
                    switch (ct)
                    {
                        case "cbiTXT":
                            fileToSave += ".txt";
                            c.ConvertToTxt(SourceTb.Text, fileToSave);
                            break;
                        case "cbiDOC":
                            fileToSave += ".doc";
                            c.ConvertToDoc(SourceTb.Text, fileToSave);
                            break;
                        case "cbiDOCX":
                            fileToSave += ".docx";
                            c.ConvertToDocX(SourceTb.Text, fileToSave);
                            break;
                        case "cbiHTML":
                            fileToSave += ".html";
                            c.ConvertToHtml(SourceTb.Text, fileToSave);
                            break;
                        case "cbiMOBI":
                            fileToSave += ".mobi";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPRC":
                            fileToSave += ".prc";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                    }
                }
            }
            catch { throw; }
        }

        private void HtmlConvert()
        {
            try
            {
                string fileExt = System.IO.Path.GetExtension(SourceTb.Text);
                string fileToSave = this.SaveToTb.Text + "\\" + System.IO.Path.GetFileNameWithoutExtension(SourceTb.Text);
                if (fileExt == ".html" || fileExt == ".htm")
                {
                    HtmlConverter c = new HtmlConverter();
                    string ct = (this.ConvertToCb.SelectedItem as ComboBoxItem).Name;
                    switch (ct)
                    {
                        case "cbiTXT":
                            fileToSave += ".txt";
                            c.ConvertToTxt(SourceTb.Text, fileToSave);
                            break;
                        case "cbiDOC":
                            fileToSave += ".doc";
                            c.ConvertToDoc(SourceTb.Text, fileToSave);
                            break;
                        case "cbiDOCX":
                            fileToSave += ".docx";
                            c.ConvertToDocX(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPDF":
                            fileToSave += ".pdf";
                            c.ConvertToPdf(SourceTb.Text, fileToSave);
                            break;
                        case "cbiMOBI":
                            fileToSave += ".mobi";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPRC":
                            fileToSave += ".prc";
                            c.ConvertToMobi(SourceTb.Text, fileToSave);
                            break;
                    }
                }
            }
            catch { throw; }
        }

        private void MobiConvert()
        {
            try
            {
                string fileExt = System.IO.Path.GetExtension(SourceTb.Text);
                string fileToSave = this.SaveToTb.Text + "\\" + System.IO.Path.GetFileNameWithoutExtension(SourceTb.Text);
                if (fileExt == ".mobi" || fileExt == ".prc")
                {
                    MobiConverter c = new MobiConverter();
                    string ct = (this.ConvertToCb.SelectedItem as ComboBoxItem).Name;
                    switch (ct)
                    {
                        case "cbiTXT":
                            fileToSave += ".txt";
                            c.ConvertToTxt(SourceTb.Text, fileToSave);
                            break;
                        case "cbiDOC":
                            fileToSave += ".doc";
                            c.ConvertToDoc(SourceTb.Text, fileToSave);
                            break;
                        case "cbiDOCX":
                            fileToSave += ".docx";
                            c.ConvertToDocX(SourceTb.Text, fileToSave);
                            break;
                        case "cbiPDF":
                            fileToSave += ".pdf";
                            c.ConvertToPdf(SourceTb.Text, fileToSave);
                            break;
                        case "cbiHTML":
                            fileToSave += ".html";
                            c.ConvertToHtml(SourceTb.Text, fileToSave);
                            break;
                        case "cbiMOBI":
                            if (fileExt == ".prc")
                            {
                                fileToSave += ".mobi";
                                System.IO.File.Copy(SourceTb.Text, fileToSave);
                            }
                            break;
                        case "cbiPRC":
                            if (fileExt == ".mobi")
                            {
                                fileToSave += ".prc";
                                System.IO.File.Copy(SourceTb.Text, fileToSave);
                            }
                            break;
                    }
                }
            }
            catch { throw; }
        }
    }
}
